package cn.xspace.app.filemg.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.xspace.android.xafcommon.util.BaseRProxy;

import java.util.HashMap;
import java.util.Map;

import cn.xspace.app.filemg.bean.ChooseFileInfo;
import cn.xspace.app.filemg.util.FileMg;
import de.greenrobot.event.EventBus;

public class FileAllActivity extends Activity implements OnClickListener {
    protected TextView mImageRight;
    protected TextView mImageLeft;
    protected View mTitleLayout;
    private TextView tv_chose_size;
    private TextView mTitle;
    private Button btnStart;
    private ChooseFileInfo chooseInfo = new ChooseFileInfo();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(BaseRProxy.file_all);
        EventBus.getDefault().register(FileAllActivity.this);
        chooseInfo = (ChooseFileInfo) getIntent().getSerializableExtra(
                "chooseInfo");
        initTopView();
        initTextView();
    }

    protected void initTopView() {
        findViewById(BaseRProxy.r_item).setBackgroundResource(BaseRProxy.getSkinID());
        mImageLeft = (TextView) findViewById(BaseRProxy.img_left);
        mImageRight = (TextView) findViewById(BaseRProxy.img_right);
        mTitle = (TextView) findViewById(BaseRProxy.title);
        mImageRight.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mTitle.setText("文件");
    }

    private void initTextView() {
        tv_chose_size = (TextView) findViewById(BaseRProxy.tv_chose_size);
        btnStart = (Button) findViewById(BaseRProxy.btnStart);
        tv_chose_size.setText(FileMg.FormetFileSize(chooseInfo.getSize()));
        if (chooseInfo.getFileList().size() > 0) {
            btnStart.setText("发送(" + chooseInfo.getFileList().size() + ")");
        }
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        EventBus.getDefault().unregister(FileAllActivity.this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == BaseRProxy.r_my_file) {
            finish();
        } else if (v.getId() == BaseRProxy.r_memory) {
            Intent intent1 = new Intent(FileAllActivity.this,
                    FolderActivity.class);
            intent1.putExtra("PATH", Environment.getRootDirectory().getParent());
            intent1.putExtra("chooseInfo", chooseInfo);
//			startActivityForResult(intent1, 120);
            startActivity(intent1);
        } else if (v.getId() == BaseRProxy.r_sdcard) {
            Intent intent1 = new Intent(FileAllActivity.this,
                    FolderActivity.class);
            intent1.putExtra("PATH", Environment.getExternalStorageDirectory()
                    .getPath());
            intent1.putExtra("chooseInfo", chooseInfo);
//			startActivityForResult(intent1, 120);
            startActivity(intent1);
        } else if (v.getId() == BaseRProxy.btnStart) {
            if (chooseInfo.getFileList().size() == 0) {
                Toast.makeText(this, "请选择文件", Toast.LENGTH_SHORT).show();
            } else {
//				Intent mIntent = new Intent();
//				mIntent.putExtra("chooseInfo", chooseInfo);
//				mIntent.putExtra("isSend", true);
//				setResult(RESULT_OK, mIntent);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("type", "FileAllActivity");
                map.put("chooseInfo", chooseInfo);
                map.put("isSend", true);
                EventBus.getDefault().post(map);
                finish();
            }

        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 120 && resultCode == RESULT_OK) {
            chooseInfo = (ChooseFileInfo) data
                    .getSerializableExtra("chooseInfo");
            boolean isSend = data.getBooleanExtra("isSend", false);
            if (isSend) {
                Intent mIntent = new Intent();
                mIntent.putExtra("chooseInfo", chooseInfo);
                mIntent.putExtra("isSend", true);
                setResult(RESULT_OK, mIntent);
                finish();
            } else {
                tv_chose_size.setText(FileMg.FormetFileSize(chooseInfo
                        .getSize()));
                if (chooseInfo.getFileList().size() > 0) {
                    btnStart.setText("发送(" + chooseInfo.getFileList().size()
                            + ")");
                } else {
                    btnStart.setText("发送");
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        Intent mIntent = new Intent();
        mIntent.putExtra("chooseInfo", chooseInfo);
//		setResult(RESULT_OK, mIntent);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", "FileAllActivity");
        map.put("chooseInfo", chooseInfo);
        EventBus.getDefault().post(map);
        finish();
    }

    public void onEventMainThread(Map<String, Object> map) {
        if ("FolderActivity".equals(map.get("type"))) {
            chooseInfo = (ChooseFileInfo) map.get("chooseInfo");
            boolean isSend = (Boolean) map.get("isSend");
            if (isSend) {
//				Intent mIntent = new Intent();
//				mIntent.putExtra("chooseInfo", chooseInfo);
//				mIntent.putExtra("isSend", true);
//				setResult(RESULT_OK, mIntent);
                map = new HashMap<String, Object>();
                map.put("type", "FileAllActivity");
                map.put("chooseInfo", chooseInfo);
                map.put("isSend", true);
                EventBus.getDefault().post(map);
                finish();
            } else {
                tv_chose_size.setText(FileMg.FormetFileSize(chooseInfo
                        .getSize()));
                if (chooseInfo.getFileList().size() > 0) {
                    btnStart.setText("发送(" + chooseInfo.getFileList().size()
                            + ")");
                } else {
                    btnStart.setText("发送");
                }
            }
        }
    }
}
